package vesion2.object.special;

import vesion2.object.Circle;
import vesion2.object.GameObject;
import vesion2.object.Point;
import vesion2.object.Rectangle;

public class Topology {

    public static boolean isIntersect(GameObject object1, GameObject object2){
        if(object1 instanceof Circle)
        {
            if(object2 instanceof Circle)
                return isIntersect((Circle) object1,(Circle)object2);
            else
                return isIntersect((Rectangle)object2, (Circle) object1);
        }
        if(object1 instanceof Rectangle){
            if(object2 instanceof Circle)
                return isIntersect((Rectangle) object1,(Circle)object2);
            else
                return isIntersect((Rectangle) object1,(Rectangle) object2);
        }

        return false;
    }
    public static boolean isIntersect(Rectangle rectangle,Rectangle rectangle1){
        boolean px=false;
        if(rectangle.getLeft_up().getX()<=rectangle1.getLeft_up().getX()) {
            if (rectangle.getRight_down().getX() >rectangle1.getLeft_up().getX())
                px = true;
        }
        else
        {
            if(rectangle.getLeft_up().getX()<rectangle1.getRight_down().getX())
                px=true;
        }

        if(px){
            if(rectangle.getLeft_up().getY()<=rectangle1.getLeft_up().getY()) {
                if (rectangle.getRight_down().getY() > rectangle1.getLeft_up().getY())
                    return true;
            }
            else
            {
                if(rectangle.getLeft_up().getY()<rectangle1.getRight_down().getY())
                    return true;
            }
        }
        return false;
    }

    public static boolean isIntersect(Rectangle rectangle,Circle circle){
        if(Math.pow(circle.getRadius(),2)>
                getDistance(getNearestPoint(rectangle,circle.getCenter()),
                        circle.getCenter()))
            return true;

        Rectangle rec=new Rectangle(new Point(rectangle.getLeft_up().getX()-circle.getRadius(),rectangle.getLeft_up().getY()),
                                    new Point(rectangle.getRight_down().getX()+circle.getRadius(),rectangle.getRight_down().getY()));

        Rectangle rec1=new Rectangle(new Point(rectangle.getLeft_up().getX(),rectangle.getLeft_up().getY()- circle.getRadius()),
                                     new Point(rectangle.getRight_down().getX(),rectangle.getRight_down().getY()+ circle.getRadius()));
        return isIntersect(circle.getCenter(),rec1)||isIntersect(circle.getCenter(),rec);
    }

    public static boolean isIntersect(Circle circle,Circle circle1){
        if(getDistance(circle.getCenter(),circle1.getCenter())<Math.pow(circle.getRadius()+ circle1.getRadius(),2))
            return true;
        return false;
    }

    public static boolean isIntersect(Point point,Rectangle rectangle){
        if(point.getX()>rectangle.getLeft_up().getX()&&point.getX()<rectangle.getRight_down().getX()
           &&point.getY()>=rectangle.getLeft_up().getY()&&point.getY()<=rectangle.getRight_down().getY())
            return true;
        else
            return false;
    }

    private static Point getNearestPoint(Rectangle rectangle,Point point){
        int nearestX,nearestY;

        if(point.getX()>rectangle.getLeft_up().getX()+rectangle.getWidth()/2)
            nearestX=rectangle.getRight_down().getX();
        else
            nearestX=rectangle.getLeft_up().getX();
        if(point.getY()>rectangle.getLeft_up().getY()+rectangle.getHeight()/2)
            nearestY=rectangle.getRight_down().getY();
        else
            nearestY=rectangle.getLeft_up().getY();

        return new Point(nearestX,nearestY);
    }

    private static double getDistance(Point point1,Point point2){
        return Math.pow((point1.getX()- point2.getX()),2)+ Math.pow((point1.getY()- point2.getY()),2);
    }
}
